package com.sunnada.edu.business.salaryManage.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sunnada.edu.business.salaryManage.dao.RewardDao;
import com.sunnada.edu.business.setProcess.service.ProcessService;
import com.sunnada.edu.system.sysrole.service.RoleMethods;
import com.sunnada.edu.system.pub.cache.ConfigCache;
import com.sunnada.edu.system.pub.common.AppContext;
import com.sunnada.edu.system.pub.common.Common;
import com.sunnada.edu.system.pub.dao.PubDao;
import com.sunnada.edu.system.pub.util.DateUtil;



@Service
@Transactional
public class RewardServiceImpl implements RewardService {
	@Resource
	RewardDao rewardDao;
	
	@Resource
	ProcessService processService;
	
	@Override
	public int getWorkerCots(HashMap<String, String> params) {
		return rewardDao.getWorkerCots(params);
	}
	@Override
	public List<?> findWorkers(HashMap<String, String> params) {
		return rewardDao.findWorkers(params);
	}

	@Override
	public String rewardDel(HashMap<String, String> params) {
		String msg = null;
		int num = rewardDao.rewardDel(params);
		if(num==0){
			msg = "删除失败!";
		}else{
			updaterewardStatus();
		}
		return msg;
	}

	@Override
	public String rewardAdd(HashMap<String, String> params) {
		String msg = "";
		String recId = UUID.randomUUID().toString();
		params.put("recId", recId);
		params.put("applyTime", DateUtil.getNowTime());
		int num = rewardDao.rewardAdd(params);
		if(num==0){
			msg = "添加失败!";
		}else{
			params.put("goDetailPage", "rewardApply");
			msg = processService.startProcess(params);
		}
		return msg;
	}

	/*
	 * 判断关键字在数据库是否已经存在
	 */
	private int judgerewardUnique(HashMap<String, String> params) {
		List<HashMap<String, String>> list = rewardDao.judgeRewardUnique(params);
		return list.size();
	}
	@Override
	public String rewardUpdate(HashMap<String, String> params) {
		String msg = "";
		int num = rewardDao.rewardUpdate(params);
		if(num==0){
			msg = "修改失败!";
		}else{
			updaterewardStatus();
		}
		return msg;
		
	}

	@Override
	public Map getRewardByRecId(HashMap<String, String> params) {
		return rewardDao.getRewardByRecId(params);
	}
	
	private void updaterewardStatus(){
		PubDao dao = AppContext.getInstance().getPubDao();
		String sql = "update S_DICT set rec_value = '2' where rec_key = 'reward' and rec_type = 'reward'";
		dao.update(sql);
	}
	@Override
	public List<?> getRewardType(HashMap<String, String> params) {
		return rewardDao.getRewardType(params);
	}
	@Override
	public List<?> getRewardLevel(HashMap<String, String> params) {
		return rewardDao.getRewardLevel(params);
	}
	@Override
	public List<?> getRewardApplyByRecid(HashMap<String, String> params) {
		return rewardDao.getRewardApplyByRecid(params);
	}
	@Override
	public List<?> getProcessConditionByrecid(HashMap<String, String> params) {
		return rewardDao.getProcessConditionByrecid(params);
	}
	@Override
	public String audit(HashMap<String, String> params) {
		String msg = "";
		msg = processService.audit(params);
		return msg;
	}
	@Override
	public int getProjectCots(HashMap<String, String> params) {
		return rewardDao.getProjectCots(params);
	}
	@Override
	public List<?> findProjects(HashMap<String, String> params) {
		return rewardDao.findProjects(params);
	}
}
